// pages/tixain/tixain.js
const app =getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    getUserYUUrl: app.data.url +'/mapi/newfenxiao/getuseryue',
    gettixianurl: app.data.url +'/mapi/newfenxiao/getcash',
    mome:0,
    max:0,
    kongzhi:true,
    weixin:'',
    guize:[],
    min:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var user = wx.getStorageSync('user');
    var that = this;
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: that.data.getUserYUUrl,
      method:'post',
      data:{token:user.token},
      success:function(res){
        var data = res.data;
        console.log(data);
        if(data.code==200){
          console.log(data.data);
          var user = data.data.user;
          var weixin = data.data.weixin.dir;
          var guize = data.data.guize;
          that.setData({ max: parseFloat(data.data.user.balance), weixin: data.data.weixin.dir, guize:data.data.guize,min:data.data.min});
        }else{
          wx.showToast({
            title: data.msg,
            icon:'none'
          })
        }
        wx.hideLoading();
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },
  fuziweixin:function(e){
    var str = e.currentTarget.dataset.str;
    wx.setClipboardData({
      data: str,
      success:function(e){
        console.log(e);
      }
    })
  },
  //全部提现
  quanbu:function(){
    console.log('haha')
    var max = this.data.max;
    this.setData({mome:max});
  },
  //input框设置值
  gaibian:function(e){
    var value = e.detail.value;
    //value = parseFloat(value);
    //console.log(value);
    value = value.replace(/^0/,'');
    this.setData({mome:value});
  },
  //提现函数
  querentixian:function(){
    var kongzhi = this.data.kongzhi;
    if(!kongzhi){
      return;
    }
    var that = this;
    var mome = parseFloat(this.data.mome);
    var max  = this.data.max;
    var min = this.data.min;
    if(mome==0){
      that.setData({ kongzhi:true});
      wx.showToast({
        title: '请输入要提现的金额',
        icon:'none'
      });
      return;
    }
    if(mome<min){
      that.setData({ kongzhi: true });
      wx.showToast({
        title: '请输入最少输入'+min,
        icon: 'none'
      });
      return;
    }
    if(mome>max){
      that.setData({ kongzhi: true });
      wx.showToast({
        title: '请输入正确金额',
        icon: 'none'
      });
      return;
    }
    wx.showModal({
      title: '提示',
      content: '确认提现吗？',
      success:function(e){
        console.log(e);
        if (e.confirm){
          wx.showModal({
            title:'提示',
            content:'本司正在努力和微信接洽提现接口，请暂时添加财务微信完成提现',
            success:function(res){
              if(res.confirm){
                var user = wx.getStorageSync('user');
                wx.showLoading({
                  title: '处理中',
                })
                that.setData({ kongzhi: false });
                wx.request({
                  url: that.data.gettixianurl,
                  method: 'post',
                  data: { token: user.token, money: mome },
                  success: function (res) {
                    var data = res.data;
                    console.log(data);
                    if (data.code == 200) {
                      var user = data.data;
                      wx.showToast({
                        title: '提现成功',
                      })
                      that.setData({ max: user.balance });
                      wx.setStorageSync('user', user);
                    } else {
                      wx.showToast({
                        title: data.msg,
                        icon: 'none'
                      })
                    }
                    that.setData({ kongzhi: true });
                    wx.hideLoading();
                  }
                })
              }else{
                that.setData({ kongzhi: true });
              }
            }
          })
        }else{
          that.setData({ kongzhi: true });
        }
      }
    })

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      mome: 0,
      max: 0,
      kongzhi: true});
      this.onShow();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var user = wx.getStorageSync('user');
    var titles = wx.getStorageSync('titles');
    return {
      title: titles,
      path: '/pages/index/index?scene' + user.id,
    };
  }
})